<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vuex加减的例子-actions版本-异步</title>
</head>
<body>
    <div id="app">
        <button @click="showStore">显示仓库的内容</button>
        <h1>{{this.$store.state.counter}}</h1>
        <button @click="add">加1</button>
    </div>
     <!-- 1. 引入 vuex 依赖 -->
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script src="./node_modules/axios/dist/axios.js"></script>
    <script src="./node_modules/vuex/dist/vuex.js"></script>
    <script>
       /*2.创建仓库*/
       let store = new Vuex.Store({
           //2.1仓库中的数据
           state:{
               counter:888,
           },
           //3.改变值的操作
           mutations:{
               increment:function(state, count){
                   state.counter+= count;
               }
           },
           //5.提交异步操作
           actions:{
               asyncIncrement:function(stroe){
                   //6. 执行异步的操作
                   axios.get("http://linweiqin.cn:8001/statis/admin/2020-10-17/count").then(res=>{
                       console.log(res.data.count);
                       stroe.commit("increment",res.data.count)
                       
                   }).catch(err=>console.log(err));
               }
           }
        });
        let vm = new Vue({
            store,
            el:"#app",
            methods: {
                showStore:function(){
                    console.log(this.$store)
                },
                // 4.点击分发值的改变
                add:function(){
                    /**
                     * this.$store.state.num+=1直接修改
                     * 提交 commit 修改数据
                     * this.$store.commit("increment")
                    */
                   
                   this.$store.dispatch("asyncIncrement")
                }
            },
        })
    </script>
</body>
</html>